package TAIC.util;

import java.io.File;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

import TAIC.Classifier.Model;

public class GenImageModel {
	HashMap < String, int[]> dict = new HashMap < String, int[] > () ;	
	HashMap < String, int[]> sortedDict = new HashMap < String, int[] > () ;

	/**
	 * @param args
	 * This program is used to generate the ImageModel for each dataset using their respective percetage threshold
	 */
	public static void main(String[] args) throws Exception {
		if ( args.length != 1 ) {
			System.out.println ( "please input the configFile"  ) ;
			return ;
		}
		( new GenImageModel( "WordImageDict.txt" )).GenAll( args [ 0 ]) ;
	}
	
	public GenImageModel ( String dictFile ) throws Exception { 
		Scanner scanner = new Scanner ( new File ( dictFile )) ;
		while(  scanner.hasNext() ) { 
			String word = scanner.next(); 
			int [] p = new int [ 800 ] ;
			for ( int i = 0 ; i < 800 ; i ++ ) p [ i ] = scanner.nextInt() ;
			dict.put( word,  p ) ;
			
			int [] q = new int [ 800 ] ;
			for ( int i = 0 ; i < 800 ; i ++ ) q [ i ] = p [ i ];
			Arrays.sort( q ) ; 
			sortedDict.put( word,  q ) ; 
		}
		scanner.close() ;		
	}	
	
	public void GenAll ( String configFile ) throws Exception 	{
		Scanner scanner = new Scanner ( new File ( configFile ) ) ; 
		while ( scanner.hasNext() ) {
			String FolderName = scanner.next() ;
			int percentage = scanner.nextInt() ; 
			GenOne ( FolderName + "\\Model.txt", FolderName + "\\ImageModel.txt", percentage ) ; 
		}
		scanner.close(); 
	}
	
	public void GenOne ( String textModelFile, String outputFile, int percentage ) throws Exception {
		Model model ;
		String [] wordList;
		
		Scanner scanner = new Scanner ( new File ( textModelFile )) ;
		scanner.next() ; 
		int maxWords = scanner.nextInt() ;
		wordList = new String [ maxWords ] ;
		for ( int i = 0 ; i < maxWords ; i ++ ) wordList [ i ] = scanner.next() ; 
		scanner.next() ;
		int maxClass = scanner.nextInt() ;

		model = new Model ( maxClass, maxWords ) ; 
		for ( int i = 0 ; i < maxWords ; i ++ ) model.wc [ i ] = new double [ maxClass ] ; 
		for ( int i = 0 ; i < maxClass ; i ++ ) { 
			scanner.next() ;
			scanner.next(); 
			model.c[ i ] = scanner.nextDouble ();
			scanner.next() ;
			for ( int j = 0 ; j < maxWords ; j ++ ) model.wc [ j ][ i ]	= scanner.nextDouble() ;
		}
		scanner.close() ;

		//System.out.println(sortedDict.get( wordList[ 1 ])[ ( 100 - percentage ) * 8 ]);
		///////////  Generate the Image Model for this text Model ///////////
		int classes = model.maxClass ;
		int totalWords = model.features ;
		PrintStream fout = new PrintStream ( new File ( outputFile ) ) ;
		fout.println ( "Total_Classes: " + classes ) ;
		for ( int i = 0 ; i < classes ; i ++ ) {
			double [] p = new double [ 800 ] ;
			for ( int j = 0; j < totalWords; j ++ ) {
				double [] q = getTopPercentage ( dict.get( wordList[ j ] ) ,
						sortedDict.get( wordList[ j ]), percentage ) ;
				for ( int k = 0 ; k < 800; k ++ ) p [ k ] += model.wc[j][i] * q [ k ] ; 
			}
			for ( int j = 0 ; j < 800; j ++ ) fout.printf( " %.8f", p [ j ]   ) ;
			fout.println(); 
		}
		fout.close() ;		
	}
	
	public double[] getTopPercentage ( int [] p , int sortedP [] , int percentage ) {
		double [] q = new double [ 800 ] ;
		double threshold = sortedP [ ( 100 - percentage ) * 8 ] ;

		int total = 0 ; 
		for ( int i = 0 ; i < 800 ; i ++ ) 
			if ( p [ i ] >= threshold ) total += ( q[ i ] = p [ i ]) ; 
		for ( int i = 0 ; i < 800 ; i ++ ) q [ i ] = ( 1.0 + q [ i ] ) / ( total + 800 ) ;
		return q ; 
	}
}
